package outils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author gervaila
 */
public class ImageOutils {

	/**
	 *
	 */
	public ImageOutils() {
		super();
	}

	/**
	 *
	 * @param bi
	 * @return une copie de l'image 'bi'
	 */
	public static BufferedImage deepCopy(BufferedImage bi) {
		ColorModel cm = bi.getColorModel();
		boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
		WritableRaster raster = bi.copyData(null);
		return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
	}

	/**
	 *
	 * @param bi la bufferedImage à vider
	 * @return le graphics2D de la bufferedImage vidée
	 */
	public static Graphics2D viderBufferedImage(BufferedImage bi) {
		Graphics2D gr = (Graphics2D) bi.createGraphics();
		gr.setBackground(new Color(255, 255, 255, 0));
		gr.clearRect(0, 0, bi.getWidth(), bi.getHeight());
		return gr;
	}

	/**
	 *
	 * @param bi
	 * @param facteur
	 * @return une copie du canal alphe de l'image 'bi'
	 */
	public static BufferedImage copyAlpha(BufferedImage bi, float facteur) {
		BufferedImage bi2 = deepCopy(bi);
		for (int x = 0; x < bi2.getWidth(); x++) {
			for (int y = 0; y < bi2.getHeight(); y++) {
				int couleur = bi2.getRGB(x, y);
				int alpha = (couleur >> 24) & 0xFF;
				alpha *= facteur;
				couleur = (couleur & 0x00FFFFFF) | (alpha << 24);
				bi2.setRGB(x, y, couleur);
			}
		}
		return bi2;
	}

	/**
	 *
	 * @param bi
	 * @param scaleValue
	 * @return une BufferedImage redimensionné selon le facteur 'scaleValue'
	 */
	public static BufferedImage scale(BufferedImage bi, double scaleValue) {
		AffineTransform tx = new AffineTransform();
		tx.scale(scaleValue, scaleValue);
		AffineTransformOp op = new AffineTransformOp(tx,
				AffineTransformOp.TYPE_BILINEAR);
		BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * scaleValue),
				(int) (bi.getHeight() * scaleValue),
				bi.getType());
		return op.filter(bi, biNew);

	}

	/**
	 *
	 * @param pan
	 * @param lab
	 */
	public static void ajouterJLabelPanel(JPanel pan, JLabel lab) {
		javax.swing.GroupLayout apercuMapLayout = new javax.swing.GroupLayout(pan);
		pan.setLayout(apercuMapLayout);
		apercuMapLayout.setHorizontalGroup(
				apercuMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(apercuMapLayout.createSequentialGroup().addComponent(lab).addGap(0, 0, Short.MAX_VALUE)));
		apercuMapLayout.setVerticalGroup(
				apercuMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(apercuMapLayout.createSequentialGroup().addComponent(lab).addGap(0, 0, Short.MAX_VALUE)));
	}

	/**
	 * copie l'image d'origine dans une nouvelle image puis en dessine le
	 * contour
	 *
	 * @param image
	 * @param couleur
	 * @param epaisseur
	 * @return l'image générée
	 */
	public static BufferedImage dessinerContour(BufferedImage image, Color couleur, int epaisseur) {
		BufferedImage image2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = image2.createGraphics();
		g.setColor(couleur);
		int largeurContour = 2;
		for (int x = 0; x < image.getWidth(); x++) {
			for (int y = 0; y < image.getHeight(); y++) {
				convolution:
				for (int dx = -largeurContour; dx <= largeurContour; dx++) {
					for (int dy = -largeurContour; dy <= largeurContour; dy++) {
						if (x + dx >= 0 && x + dx < image.getWidth()
								&& y + dy >= 0 && y + dy < image.getHeight()) {
							int alpha = (image.getRGB(x + dx, y + dy) >> 24) & 0xFF;
							if (alpha > 10) {
								g.drawRect(x, y, 1, 1);
								break convolution;
							}
						}
					}
				}
			}
		}
		g.drawImage(image, null, 0, 0);
		return image2;
	}
}
